package com.wanmei.controller.user;

import com.wanmei.exception.WanmeiException;
import com.wanmei.pojo.Exception_Msg;
import com.wanmei.pojo.JsonResult;
import com.wanmei.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

@Controller
@RequestMapping("user")
public class ForgetController {
    @Autowired
    private UserService userService;

    /**
     * 忘记密码
     * @return
     */
    @RequestMapping("/info/forget")
    public String forgetView() {
        return "forget";
    }

    /**
     * 找回密码 发送邮件
     * @param req
     * @return
     * @throws ParseException
     * @throws WanmeiException
     * @throws MessagingException
     */
    @RequestMapping("/info/sendEmail.json")
    @ResponseBody
    public JsonResult sendEmailToFindPwd(HttpServletRequest req) throws ParseException, WanmeiException, MessagingException {
        JsonResult result = new JsonResult();
        String email = req.getParameter("email");
        int isSuccess = userService.saveEmailActiveInfo(email, "find_password");
        result.setCode(isSuccess);
        return result;
    }

    /**
     * 检查验证码,还有密码
     * @param req
     * @return
     * @throws WanmeiException
     * @throws ParseException
     */
    @RequestMapping("/info/updatePwd")
    @ResponseBody
    public JsonResult checkIncodeByFindPwd(HttpServletRequest req) throws WanmeiException, ParseException {
        JsonResult result = new JsonResult();
        String email = req.getParameter("email");
        String incode = req.getParameter("incode");
        String password= req.getParameter("password");
        String rePassword =req.getParameter("rePassword");
        int isSuccess = userService.checkActiveInfoByCode(email,incode,"find_password",password,rePassword);
        if (isSuccess==1){
            result.setCode(1);
        }
        return  result;
    }


}
